package com.xu.base.exception;

import com.xu.base.enums.ResponseCodeEnum;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * 业务通用异常类
 *
 * @Author: 许乾伟
 * @Date: 2023/12/5  16:31
 */
@Getter
public class ServiceException extends RuntimeException {
    private final Integer code;

    /**
     * 通过错误码构建异常
     * @param responseCode
     */
    public ServiceException(ResponseCodeEnum responseCode) {
        super(responseCode.getDesc());
        this.code = responseCode.getCode();
    }
    /**
     * 通过错误码及错误消息构建异常
     * @param responseCode
     */
    public ServiceException(ResponseCodeEnum responseCode, String msg) {
        super(StringUtils.hasText(msg) ? msg : responseCode.getDesc());
        this.code = responseCode.getCode();
    }
}
